//
//  CenterViewController.m
//  SportRing
//
//  Created by 微我网络 on 15/1/23.
//  Copyright (c) 2015年 HotOne. All rights reserved.
//

#import "CenterViewController.h"
#import "ActiveCircleView.h"
#import "UIViewController+MMDrawerController.h"
#import "MJRefresh.h"
#import <UICountingLabel/UICountingLabel.h>
#import "ChartViewController.h"
#import "MoonSelectView.h"
#import "WeatherView.h"

#import "SRViewSetting.h"
#import "SRMainView.h"
#import "SRSelectDateView.h"
#import "SRSetTargetView.h"
#import "SRSportShareViewController.h"
#import "SRSportDetailViewController.h"
#import "SRManager.h"
#import "SRTaskService.h"
#import "SRDB.h"
#import "SRSportBleService.h"
#import "SRSportNetService.h"
#import "NSDate+Additionals.h"
#import "SRSportDetailTableViewCell.h"
#import "NSDate+Additionals.h"
#import "SRSportTask+Cate.h"
#import "GetLocation.h"

typedef NS_ENUM(NSInteger, DataType) {
    DataTypeSport,
    DataTypeSleep
};

@interface CenterViewController () <UIScrollViewDelegate,GetLocationDelegate>

@property (nonatomic,strong) ActiveCircleView *circle0;
@property (nonatomic,strong) ActiveCircleView *circle1;
@property (nonatomic,strong) UIScrollView *subScroll0;
@property (nonatomic,strong) UIScrollView *subScroll1;
@property (nonatomic,strong) UIImageView *ground_day;
@property (nonatomic,strong) UIImageView *ground_night;
//@property (nonatomic,strong) WeatherView *weatherView;
@property (nonatomic,strong) MoonSelectView *moonView;

@property (nonatomic,strong) UICountingLabel *stepsCount_day;
@property (nonatomic,copy) NSString *steps_str;
@property (nonatomic,strong) UILabel *target_day;
@property (nonatomic,copy) NSString *target_str;
@property (nonatomic,strong) UIButton *synchro_day;
@property (nonatomic,strong) UILabel *calorie;
@property (nonatomic,copy) NSString *calorie_str;
@property (nonatomic,copy) NSString *fat_str;
@property (nonatomic,strong) UILabel *distance;
@property (nonatomic,copy) NSString *distance_str;

@property (nonatomic,strong) UILabel *temperature;//实时温度
@property (nonatomic,strong) UILabel *glass;//气压
@property (nonatomic,strong) UILabel *elevation;//海拔
@property (nonatomic,strong) UILabel *myLocation;//地理位置

@property (nonatomic,strong) UILabel *interval_night;
@property (nonatomic,copy) NSString *hour_str;
@property (nonatomic,copy) NSString *minute_str;
@property (nonatomic,strong) UILabel *time_night;
@property (nonatomic,strong) UIButton *synchro_night;
//@property (nonatomic,strong) NSMutableArray *sleepDetail;

@property (nonatomic,copy) NSString *deepSleep;
@property (nonatomic,copy) NSString *lightSleep;
@property (nonatomic,copy) NSString *activeSleep;

@property (nonatomic) DataType type;

@property (nonatomic, strong) SRSleepingViewModel *sleepViewModel;
@property (nonatomic, strong) SRSportViewModel *sportViewModel;
@property (nonatomic, strong) SRDay *srday;

@property (nonatomic, strong) GetLocation *location;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation CenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _location = [[GetLocation alloc] init];
    _location.delegate = self;
    [_location performSelector:@selector(startLocation) withObject:nil afterDelay:2];
    
    _type = 0;
    _hour_str   = @"00";
    _minute_str = @"00";
    _calorie_str= @"0";
    _fat_str    = @"0";
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self addObservers];
    
    _ground_night = [[UIImageView alloc] initWithFrame:self.view.frame];
    _ground_night.image = [UIImage imageNamed:@"center_ground_night"];
    [self.view addSubview:_ground_night];
    
    _ground_day = [[UIImageView alloc] initWithFrame:self.view.frame];
    _ground_day.image = [UIImage imageNamed:@"center_ground_day"];
    [self.view addSubview:_ground_day];
    
    UIButton *left_button = [UIButton buttonWithType:UIButtonTypeCustom];
    left_button.frame = CGRectMake(0, 0, 22, 17);
    [left_button setImage:[UIImage imageNamed:@"left_menu"] forState:UIControlStateNormal];
    [left_button addTarget:self action:@selector(leftBarButtonItemPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:left_button];
    self.navigationItem.leftBarButtonItem = left;
    
    UIButton *right_button = [UIButton buttonWithType:UIButtonTypeCustom];
    right_button.frame = CGRectMake(0, 0, 27, 16);
    [right_button setImage:[UIImage imageNamed:@"right_menu"] forState:UIControlStateNormal];
    [right_button addTarget:self action:@selector(rightBarButtonItemPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *right= [[UIBarButtonItem alloc] initWithCustomView:right_button];
    self.navigationItem.rightBarButtonItem = right;
    
    [self initializeUserInterface];
//    [self preLoadDayDataForDate:[NSDate date]];
    [self getDayDataByDate:[NSDate date]];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(update) userInfo:nil repeats:YES];
    
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"first"]) {
        UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
        view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"首页引导图"]];
        view.tag = 2;
        [self.view addSubview:view];
        [[NSUserDefaults standardUserDefaults] setObject:@"first" forKey:@"first"];
    }

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (touch.view.tag == 2) {
        [touch.view removeFromSuperview];
    }
}

- (void)update {
    
    [_location startLocation];
    
    [[[[SRSportBleService readCurrentTaskData] flattenMap:^RACStream *(id value) {
        return [SRSportBleService readCurrentSportData];
    }] flattenMap:^RACStream *(id value) {
        return [SRSportBleService readSleepData];
    }] subscribeNext:^(id x) {
        [[SRManager sharedInstance] readyToStartUp];
    }];
}

- (void)dealloc {
    
    [_timer invalidate];
}

- (void)leftBarButtonItemPressed:(UIButton *)sender {
    
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

- (void)rightBarButtonItemPressed:(UIButton *)sender {
    
    ChartViewController *vc = [[ChartViewController alloc] init];
    [self.mm_drawerController presentViewController:vc animated:YES completion:nil];
}

- (void)addObservers {
    
    @weakify(self);
    [[NSNotificationCenter defaultCenter] addObserverForName:NEW_CURRENT_TASK_DATA_NOTIFY
                                                      object:nil
                                                       queue:nil
                                                  usingBlock:^(NSNotification *note) {
                                                      [self_weak_ getDayDataByDate:[NSDate date]];
                                                      NSLog(@"%@",[_sportViewModel description]);
                                                  }];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:NEW_CURRENT_SPORT_DATA_NOTIFY
                                                      object:nil
                                                       queue:nil
                                                  usingBlock:^(NSNotification *note) {
                                                      [self_weak_ getDayDataByDate:[NSDate date]];
                                                      
                                                  }];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:NEW_SLEEP_DATA_NOTIFY
                                                      object:nil
                                                       queue:nil
                                                  usingBlock:^(NSNotification *note) {
                                                      [self_weak_ getDayDataByDate:[NSDate date]];
                                                      
                                                  }];
}

- (void)updateDataWithScrollView:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    self.timer = nil;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:60
                                                  target:self
                                                selector:@selector(blueTimeout)
                                                userInfo:nil
                                                 repeats:NO];
    @weakify(self);
    [[[[SRSportBleService readCurrentTaskData] flattenMap:^RACStream *(id value) {
        return [SRSportBleService readCurrentSportData];
    }] flattenMap:^RACStream *(id value) {
        return [SRSportBleService readSleepData];
    }] subscribeNext:^(id x) {
        [scrollView headerEndRefreshing];
        [self_weak_.timer invalidate];
        self_weak_.timer = nil;
    }];
}

- (void)initializeUserInterface {
    
    UIScrollView *backGroundScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-64)];
    backGroundScroll.contentSize = CGSizeMake(CGRectGetWidth(backGroundScroll.frame)*2, CGRectGetHeight(backGroundScroll.frame));
    backGroundScroll.showsVerticalScrollIndicator = NO;
    backGroundScroll.showsHorizontalScrollIndicator = NO;
    backGroundScroll.delegate = self;
    backGroundScroll.pagingEnabled = YES;
    [self.view addSubview:backGroundScroll];
    
    _subScroll0 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, backGroundScroll.frame.size.width, backGroundScroll.frame.size.height)];
    _subScroll0.contentSize = CGSizeMake(320,500);
    _subScroll0.showsVerticalScrollIndicator = NO;
    _subScroll0.showsHorizontalScrollIndicator = NO;
    __weak UIScrollView *header0 = _subScroll0;
    __weak GetLocation *weakLocation = _location;
    [_subScroll0 addHeaderWithCallback:^{
        
        [weakLocation startLocation];
        [[[[SRSportBleService readCurrentTaskData] flattenMap:^RACStream *(id value) {
            return [SRSportBleService readCurrentSportData];
        }] flattenMap:^RACStream *(id value) {
            return [SRSportBleService readSleepData];
        }] subscribeNext:^(id x) {
            [header0 headerEndRefreshing];
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [header0 headerEndRefreshing];
        });
    }];
    [backGroundScroll addSubview:_subScroll0];
    
    _subScroll1 = [[UIScrollView alloc] initWithFrame:CGRectMake(backGroundScroll.frame.size.width, 0, backGroundScroll.frame.size.width, backGroundScroll.frame.size.height)];
    _subScroll1.contentSize = CGSizeMake(320, 500);
    _subScroll1.showsVerticalScrollIndicator = NO;
    _subScroll1.showsHorizontalScrollIndicator = NO;
    __weak UIScrollView *header1 = _subScroll1;
    [_subScroll1 addHeaderWithCallback:^{
        
        [weakLocation startLocation];
        [[[[SRSportBleService readCurrentTaskData] flattenMap:^RACStream *(id value) {
            return [SRSportBleService readCurrentSportData];
        }] flattenMap:^RACStream *(id value) {
            return [SRSportBleService readSleepData];
        }] subscribeNext:^(id x) {
            [header1 headerEndRefreshing];
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [header1 headerEndRefreshing];
        });
    }];
    [backGroundScroll addSubview:_subScroll1];
    
    /**
     sons
     
     :returns: they are sons
     */
    
    _circle0 = [[ActiveCircleView alloc] initWithCenter:CGPointMake(CGRectGetMidX(self.view.frame), 150)
                                                  width:260
                                                  style:ActiveCircleStyleBlur];
    [_subScroll0 addSubview:_circle0];
    
    UILabel *walked = [[UILabel alloc] init];
    walked.bounds = CGRectMake(0, 0, 80, 20);
    walked.center = CGPointMake(CGRectGetMidX(self.view.frame), 85);
    walked.textAlignment = NSTextAlignmentCenter;
    walked.font = [UIFont systemFontOfSize:16];
    walked.text = @"今日已走";
    [_subScroll0 addSubview:walked];
    
    //HelveticaNeueLT
    _stepsCount_day = [[UICountingLabel alloc] init];
    _stepsCount_day.frame = CGRectMake(0, 0, 240, 54);
    _stepsCount_day.alpha = 0.8;
    _stepsCount_day.center = CGPointMake(CGRectGetMidX(self.view.frame), 150);
    _stepsCount_day.textAlignment = NSTextAlignmentCenter;
    _stepsCount_day.method = UILabelCountingMethodEaseInOut;
    NSAttributedString* (^UICountingLabelAttributedFormatBlock)(float value) = ^NSAttributedString* (float value) {
        
        NSString *full = [NSString stringWithFormat:@"%.0f步",value];
        
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:full];
        [string addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:56],
                                NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, full.length-1)];
        
        [string addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],
                                NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(full.length-1, 1)];
        return string;
    };
    [_stepsCount_day setAttributedFormatBlock:UICountingLabelAttributedFormatBlock];
    [_subScroll0 addSubview:_stepsCount_day];
    
//    void (^ success)(AFHTTPRequestOperation *operation, id responseObject);
//    success=^(AFHTTPRequestOperation *operation, id responseObject) {
//        if (iscancelRequst == YES) {
//            return ;
//        }
    
    [self setSteps_str:@"0000"];
    
    _target_day = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    _target_day.center = CGPointMake(CGRectGetMidX(self.view.frame), 215);
    _target_day.textAlignment = NSTextAlignmentCenter;
    _target_day.font = [UIFont systemFontOfSize:14];
//    _target_day.text = @"目标8000步";
    _target_day.textColor = [UIColor grayColor];
    [_subScroll0 addSubview:_target_day];
    SRCurrentTask * task = [SRCurrentTask findCurrentTaskCINEForMe:[SRMe currentUser]];
    [self setTarget_str:[NSString stringWithFormat:@"%@",task.target]];
    
//    _synchro_day = [UIButton buttonWithType:UIButtonTypeCustom];
//    _synchro_day.frame = CGRectMake(0, 0, 100, 20);
//    _synchro_day.center = CGPointMake(CGRectGetMidX(self.view.frame), 255);
//    _synchro_day.titleLabel.textAlignment = NSTextAlignmentCenter;
//    _synchro_day.titleLabel.font = [UIFont systemFontOfSize:12];
//    [_synchro_day setTitle:@"数据未同步" forState:UIControlStateNormal];
//    [_synchro_day setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    [_synchro_day setTitle:@"数据已同步" forState:UIControlStateSelected];
//    [_synchro_day setTitleColor:[UIColor grayColor] forState:UIControlStateSelected];
//    [_subScroll0 addSubview:_synchro_day];
    
    int x = CGRectGetWidth(self.view.frame) / 3,y = 350;
    for (int i=0; i<2; i++) {
        for (int j=0; j< 3; j++) {
            
            if (j != 2) {
                CALayer *grayLine = [[CALayer alloc] init];
                grayLine.frame = CGRectMake(x, y, 0.3, 20);
                grayLine.backgroundColor = [UIColor grayColor].CGColor;
                [_subScroll0.layer addSublayer:grayLine];
                x += CGRectGetWidth(self.view.frame) / 3;
            }
        }
        x = CGRectGetWidth(self.view.frame) / 3;
        y = y + 80;
    }
    
    _calorie = [[UILabel alloc] initWithFrame:CGRectMake(0, 385-64, 320 / 3, 80)];
    _calorie.textAlignment = NSTextAlignmentCenter;
    _calorie.font = [UIFont systemFontOfSize:12];
    _calorie.numberOfLines = 0;
    [_subScroll0 addSubview:_calorie];
    
    [self setCalorie_str:@"00"];
    [self setFat_str:@"00"];
    
    _distance = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) / 3, CGRectGetMinY(_calorie.frame), 320 / 3, 80)];
    _distance.textAlignment = NSTextAlignmentCenter;
    _distance.font = [UIFont systemFontOfSize:12];
    _distance.text = @"步行距离";
    _distance.numberOfLines = 0;
    [_subScroll0 addSubview:_distance];
    
    [self setDistance_str:@"00"];
    
    //实时温度、气压、海拔、地理位置
    _temperature = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) / 3 * 2, CGRectGetMinY(_calorie.frame), 320 / 3, 80)];
    _temperature.textAlignment = NSTextAlignmentCenter;
    _temperature.font = [UIFont systemFontOfSize:12];
    _temperature.text = @"实时温度";
    _temperature.numberOfLines = 0;
    [_subScroll0 addSubview:_temperature];
    [self setTemperatureValue:@"--"];
    
    _elevation = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) / 3 , CGRectGetMaxY(_calorie.frame), 320 / 3, 80)];
    _elevation.textAlignment = NSTextAlignmentCenter;
    _elevation.font = [UIFont systemFontOfSize:12];
    _elevation.text = @"海拔";
    _elevation.numberOfLines = 0;
    [_subScroll0 addSubview:_elevation];
    [self setElevationValue:@"0"];
    
    _glass = [[UILabel alloc] initWithFrame:CGRectMake(0 , CGRectGetMaxY(_calorie.frame), 320 / 3, 80)];
    _glass.textAlignment = NSTextAlignmentCenter;
    _glass.font = [UIFont systemFontOfSize:12];
    _glass.text = @"气压";
    _glass.numberOfLines = 0;
    [_subScroll0 addSubview:_glass];
    [self setGlassValue:@"0"];
    
    
    
    _myLocation = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) / 3 * 2, CGRectGetMinY(_glass.frame), 320 / 3, 80)];
    _myLocation.textAlignment = NSTextAlignmentCenter;
    _myLocation.font = [UIFont systemFontOfSize:12];
    _myLocation.text = @"海拔";
    _myLocation.numberOfLines = 0;
    [_subScroll0 addSubview:_myLocation];
    [self setMyLocationValue:@"--" weather:@"--"];
    
//    _weatherView = [[WeatherView alloc] initWithFrame:CGRectMake(0, _subScroll0.contentSize.height-80, _subScroll0.contentSize.width, 80)];
//    _weatherView.temperature = @"";
//    _weatherView.city = @"";
//    _weatherView.theWeather = @"";
//    [_subScroll0 addSubview:_weatherView];
    
    //the second page
    
    _circle1 = [[ActiveCircleView alloc] initWithCenter:CGPointMake(CGRectGetMidX(self.view.frame), 150)
                                                  width:260
                                                  style:ActiveCircleStylePie];
    [_subScroll1 addSubview:_circle1];
    
//    [_circle1 strokePieWithValue0:2 value1:4 value2:6];
    
    UILabel *slep = [[UILabel alloc] init];
    slep.bounds = CGRectMake(0, 0, 80, 20);
    slep.center = CGPointMake(CGRectGetMidX(self.view.frame), 85);
    slep.textAlignment = NSTextAlignmentCenter;
    slep.font = [UIFont systemFontOfSize:16];
    slep.text = @"上次睡眠";
    slep.textColor = [UIColor colorWithWhite:1.0 alpha:0.7];
    [_subScroll1 addSubview:slep];
    
    _interval_night = [[UILabel alloc] init];
    _interval_night.frame =CGRectMake(0, 0, 240, 54);
    _interval_night.center = CGPointMake(CGRectGetMidX(self.view.frame), 150);
    _interval_night.textAlignment = NSTextAlignmentCenter;
    [_subScroll1 addSubview:_interval_night];
    
    [self setHour_str:@"00"];
    [self setMinute_str:@"00"];
    
    _time_night = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 20)];
    _time_night.center = CGPointMake(CGRectGetMidX(self.view.frame), 215);
    _time_night.textAlignment = NSTextAlignmentCenter;
    _time_night.font = [UIFont systemFontOfSize:14];
    _time_night.textColor = [UIColor colorWithWhite:1 alpha:0.7];
    _time_night.text = @"数据未同步";
    [_subScroll1 addSubview:_time_night];
    
//    _synchro_night = [UIButton buttonWithType:UIButtonTypeCustom];
//    _synchro_night.frame = CGRectMake(0, 0, 100, 20);
//    _synchro_night.center = CGPointMake(CGRectGetMidX(self.view.frame), 255);
//    _synchro_night.titleLabel.textAlignment = NSTextAlignmentCenter;
//    _synchro_night.titleLabel.font = [UIFont systemFontOfSize:11];
//    [_synchro_night setTitle:@"数据未同步" forState:UIControlStateNormal];
//    [_synchro_night setTitleColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
//    [_synchro_night setTitle:@"数据已同步" forState:UIControlStateSelected];
//    [_synchro_night setTitleColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateSelected];
//    [_subScroll1 addSubview:_synchro_night];
    
    NSArray *sleepLabelName = @[@"深睡时长",@"浅睡时长",@"活动时长"];
    for (int i = 0; i < 3; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)/3*i, 385-64, CGRectGetWidth(self.view.frame)/3, 20)];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor colorWithWhite:1.0 alpha:0.7];
        label.font = [UIFont systemFontOfSize:12];
        label.text = sleepLabelName[i];
        [_subScroll1 addSubview:label];
        
        UILabel *sleepValue = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)/3*i, 385-64+34, CGRectGetWidth(self.view.frame)/3, 20)];
        sleepValue.textAlignment = NSTextAlignmentCenter;
//        sleepValue.textColor = [UIColor colorWithWhite:1.0 alpha:0.7];
//        sleepValue.font = [UIFont systemFontOfSize:12];
//        sleepValue.text = sleepLabelName[i];
        sleepValue.tag = 1100+i;
        [_subScroll1 addSubview:sleepValue];
        
        if (i > 0) {
            CALayer *line = [CALayer layer];
            line.frame = CGRectMake(CGRectGetWidth(self.view.frame)/3*i, 385-51, 0.5, 27);
            line.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.7].CGColor;
            [_subScroll1.layer addSublayer:line];
        }
    }
    
    [self setDeepSleep:@"0"];
    [self setLightSleep:@"0"];
    [self setActiveSleep:@"0"];
    
    _moonView = [[MoonSelectView alloc] initWithFrame:CGRectMake(0, _subScroll1.contentSize.height-70, _subScroll1.contentSize.width, 80)];
    _moonView.number = 0;
    [_subScroll1 addSubview:_moonView];
    
//    [self performSelector:@selector(animation) withObject:nil afterDelay:1];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    //  0  <  value  <  view.width
    CGFloat Xoffset = scrollView.contentOffset.x;
    CGFloat percentage = Xoffset/CGRectGetWidth(self.view.frame);
    
    NSLog(@"%f",Xoffset);
    
    if (percentage == 0) {
        _type = 0;
    }
    if (percentage == 1) {
        _type = 1;
    }
    
    _subScroll0.alpha = 1-percentage;
    _subScroll0.transform = CGAffineTransformMakeScale(MIN(1-percentage, 1.1), MIN(1-percentage, 1.1));
    _ground_day.alpha = 1-percentage;
    
    _subScroll1.alpha = percentage;
    _subScroll1.transform = CGAffineTransformMakeScale(MIN(percentage, 1.1), MIN(percentage, 1.1));
    _ground_night.alpha = 0.4+percentage;
    
    if (Xoffset < 0) {
        
        if (self.mm_drawerController.openSide != MMDrawerSideLeft) {
            [scrollView setScrollEnabled:NO];
            [_subScroll0 setScrollEnabled:NO];

            [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:^(BOOL finished) {
                [scrollView setScrollEnabled:YES];
                [_subScroll0 setScrollEnabled:YES];
            }];
        }
    }
    else {
        [scrollView setScrollEnabled:YES];
        [_subScroll0 setScrollEnabled:YES];
    }
}

//从数据库获取天数据
- (void)getDayDataByDate:(NSDate *)date
{
    SRSportTask * task = [SRSportTask findTaskCINEForDay:[date formattedBy_yyyyMMdd]];
    
    CGFloat process = 0;
    if (task.totalCounter.integerValue != 0) {
        process = task.completedCounter.floatValue / task.totalCounter.floatValue;
    }
    process = process > 1.0 ? 1.0 : process;
    SRAimSportViewModel *aimModel = [[SRAimSportViewModel alloc] initWithAimDistance:task.totalCounter.intValue
                                                                        chartProcess:process];
    CGFloat pressure = 0.0;
    if ([date isToday]) {
        SRCurrentData * currentData = [SRCurrentData findCurrentDataForMe:[SRMe currentUser]];
        pressure = currentData.pressure.floatValue;
        
        int steps = currentData.walkCounter.intValue + currentData.runCounter.intValue + currentData.upstairCounter.intValue;
        _sportViewModel = [[SRSportViewModel alloc] initWithSteps:steps
                                                          calorie:currentData.calori.intValue
                                                         distance:currentData.distance.floatValue / 5000000
                                                         aimModel:aimModel];
        CGFloat elevation = 0.0;
        if (pressure != 0) {
            elevation = 44330.0 *(1.0 - pow(pressure / 101.325, 0.1903));
        }
        [self setElevationValue:[NSString stringWithFormat:@"%.1f",elevation]];
        [self setGlassValue:[NSString stringWithFormat:@"%.1f",pressure * 10]];
    }
    else {
        SRDay * day = [SRDay findDayCINEForDate:date forMe:[SRMe currentUser]];
        _sportViewModel = [[SRSportViewModel alloc] initWithSteps:[day countOfSteps]
                                                          calorie:[day countOfCalorie]
                                                         distance:[day countOfDistance] / 5000000
                                                         aimModel:aimModel];
        
    }
//    [[NSUserDefaults standardUserDefaults] setObject:_sportViewModel forKey:@"IMOCSportsModel"];
    [_stepsCount_day countFrom:0 to:_sportViewModel.steps withDuration:2.0];
    SRCurrentTask * srTask = [SRCurrentTask findCurrentTaskCINEForMe:[SRMe currentUser]];
    [self setTarget_str:[NSString stringWithFormat:@"%@",srTask.target]];
    [self setCalorie_str:[NSString stringWithFormat:@"%.0f",_sportViewModel.calorie]];
    [self setFat_str:[NSString stringWithFormat:@"%.1f",_sportViewModel.calorie/9]];
    [self setDistance_str:[NSString stringWithFormat:@"%.1f",_sportViewModel.distance]];
   
    
    
    NSInteger angle = 360*_sportViewModel.steps/MAX(_sportViewModel.aimSportViewModel.aimDistance, 1);
    [_circle0 startAnimationWithAngle:angle];
    
    _srday = [SRDay findDayForDate:date forMe:[SRMe currentUser]];
    _sleepViewModel = [[SRSleepingViewModel alloc] initWithDeepTime:[SRSleep minutesOfDeepSleepForDay:_srday]
                                                         simpleTime:[SRSleep minutesOfLightSleepForDay:_srday]
                                                         activeTime:[SRSleep minutesOfActiveSleepForDay:_srday]
                                                    sleepingQuality:[SRSleep sleepLevelForDay:_srday]
                                                        sleepingTip:@""];
    
//    [[NSUserDefaults standardUserDefaults] setObject:_srday forKey:@"IMOCSleepModel"];
//    [[NSUserDefaults standardUserDefaults] synchronize];
    NSInteger totalMinutes = _sleepViewModel.deepSleepingTime + _sleepViewModel.simpleSleepingTime + _sleepViewModel.activeTime;
    [self setHour_str:[NSString stringWithFormat:@"%02ld",totalMinutes/60]];
    [self setMinute_str:[NSString stringWithFormat:@"%02ld",totalMinutes%60]];
    
    NSString *start = [_srday.sleep.startTime formattedBy_hhmm]?[_srday.sleep.startTime formattedBy_hhmm]:nil;
    NSString *end = [_srday.sleep.endTime formattedBy_hhmm]?[_srday.sleep.endTime formattedBy_hhmm]:nil;
    int level = [SRSleep sleepLevelForDay:_srday];
    [_moonView setNumber:level];
    if (start && end) {
        _time_night.text = [NSString stringWithFormat:@"%@～%@",start,end];
        NSDictionary *dic = @{@"time":_time_night.text,
                              @"level":@(level),
                              @"deepSleepingTime":@(_sleepViewModel.deepSleepingTime),
                              @"simpleSleepingTime":@(_sleepViewModel.simpleSleepingTime),
                              @"activeTime":@(_sleepViewModel.activeTime),
                              @"hour_str":_hour_str,
                              @"minute_str":_minute_str,
                              @"date":date};
        [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"sleep"];
    }
    else {
        _time_night.text = @"暂无数据";
        NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"sleep"];
        
        if (dic && [dic[@"date"] isToday]) {
            _time_night.text = dic[@"time"];
            [self setHour_str:dic[@"hour_str"]];
            [self setMinute_str:dic[@"minute_str"]];
            [_moonView setNumber:[dic[@"level"] floatValue]];
            [self setDeepSleep:[NSString stringWithFormat:@"%@",dic[@"deepSleepingTime"]]];
            [self setLightSleep:[NSString stringWithFormat:@"%@",dic[@"simpleSleepingTime"]]];
            [self setActiveSleep:[NSString stringWithFormat:@"%@",dic[@"activeTime"]]];
            [_circle1 strokePieWithValue0:[dic[@"deepSleepingTime"] floatValue] value1:[dic[@"simpleSleepingTime"] floatValue] value2:[dic[@"activeTime"] floatValue]];
            return;
        }
        
    }
    
    [self setDeepSleep:[NSString stringWithFormat:@"%.0f",_sleepViewModel.deepSleepingTime]];
    [self setLightSleep:[NSString stringWithFormat:@"%.0f",_sleepViewModel.simpleSleepingTime]];
    [self setActiveSleep:[NSString stringWithFormat:@"%.0f",_sleepViewModel.activeTime]];
    [_circle1 strokePieWithValue0:_sleepViewModel.deepSleepingTime value1:_sleepViewModel.simpleSleepingTime value2:_sleepViewModel.activeTime];
}

- (void)preLoadDayDataForDate:(NSDate *)date {
    
    SRSportTask * task = [SRSportTask findTaskCINEForDay:[date formattedBy_yyyyMMdd]];
    
    CGFloat process = 0;
    if (task.totalCounter.integerValue != 0) {
        process = task.completedCounter.floatValue / task.totalCounter.floatValue;
    }
    process = process > 1.0 ? 1.0 : process;
    SRAimSportViewModel *aimModel = [[SRAimSportViewModel alloc] initWithAimDistance:task.totalCounter.intValue
                                                                        chartProcess:process];
    
    CGFloat pressure = 0.0;
    if ([date isToday]) {
        SRCurrentData * currentData = [SRCurrentData findCurrentDataForMe:[SRMe currentUser]];
        pressure = currentData.pressure.floatValue;
        
        int steps = currentData.walkCounter.intValue + currentData.runCounter.intValue + currentData.upstairCounter.intValue;
        _sportViewModel = [[SRSportViewModel alloc] initWithSteps:steps
                                                          calorie:currentData.calori.intValue
                                                         distance:currentData.distance.floatValue / 5000000
                                                         aimModel:aimModel];
        CGFloat elevation = 0.0;
        if (pressure != 0) {
            elevation = 44330.0 *(1.0 - pow(pressure / 101.325, 0.1903));
        }
        [self setElevationValue:[NSString stringWithFormat:@"%.1f",elevation]];
        //    CGFloat glass = pressure * 90.0 / 16384.0 + 30;
        [self setGlassValue:[NSString stringWithFormat:@"%.1f",pressure * 10]];
    }
    else {
        SRDay * day = [SRDay findDayCINEForDate:date forMe:[SRMe currentUser]];
        _sportViewModel = [[SRSportViewModel alloc] initWithSteps:[day countOfSteps]
                                                          calorie:[day countOfCalorie]
                                                         distance:[day countOfDistance] / 5000000
                                                         aimModel:aimModel];
    }
    //    [[NSUserDefaults standardUserDefaults] setObject:_sportViewModel forKey:@"IMOCSportsModel"];
    [_stepsCount_day countFrom:0 to:_sportViewModel.steps withDuration:2.0];
    SRCurrentTask * srTask = [SRCurrentTask findCurrentTaskCINEForMe:[SRMe currentUser]];
    [self setTarget_str:[NSString stringWithFormat:@"%@",srTask.target]];
    [self setCalorie_str:[NSString stringWithFormat:@"%.0f",_sportViewModel.calorie]];
    [self setFat_str:[NSString stringWithFormat:@"%.1f",_sportViewModel.calorie/9]];
    [self setDistance_str:[NSString stringWithFormat:@"%.1f",_sportViewModel.distance]];
    
    
    
    NSInteger angle = 360*_sportViewModel.steps/MAX(_sportViewModel.aimSportViewModel.aimDistance, 1);
    [_circle0 startAnimationWithAngle:angle];
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"IMOCSleepModel"]) {
        _srday = [[NSUserDefaults standardUserDefaults] objectForKey:@"IMOCSleepModel"];
    }
    else {
        _srday = [SRDay findDayForDate:date forMe:[SRMe currentUser]];
    }
    _sleepViewModel = [[SRSleepingViewModel alloc] initWithDeepTime:[SRSleep minutesOfDeepSleepForDay:_srday]
                                                         simpleTime:[SRSleep minutesOfLightSleepForDay:_srday]
                                                         activeTime:[SRSleep minutesOfActiveSleepForDay:_srday]
                                                    sleepingQuality:[SRSleep sleepLevelForDay:_srday]
                                                        sleepingTip:@""];
    NSInteger totalMinutes = _sleepViewModel.deepSleepingTime + _sleepViewModel.simpleSleepingTime + _sleepViewModel.activeTime;
    [self setHour_str:[NSString stringWithFormat:@"%02ld",totalMinutes/60]];
    [self setMinute_str:[NSString stringWithFormat:@"%02ld",totalMinutes%60]];
    
    NSString *start = [_srday.sleep.startTime formattedBy_hhmm]?[_srday.sleep.startTime formattedBy_hhmm]:@"00:00";
    NSString *end = [_srday.sleep.endTime formattedBy_hhmm]?[_srday.sleep.endTime formattedBy_hhmm]:@"00:00";
    _time_night.text = [NSString stringWithFormat:@"%@～%@",start,end];
    
    int level = [SRSleep sleepLevelForDay:_srday];
    [_moonView setNumber:level];
    [self setDeepSleep:[NSString stringWithFormat:@"%.0f",_sleepViewModel.deepSleepingTime]];
    [self setLightSleep:[NSString stringWithFormat:@"%.0f",_sleepViewModel.simpleSleepingTime]];
    [self setActiveSleep:[NSString stringWithFormat:@"%.0f",_sleepViewModel.activeTime]];
    [_circle1 strokePieWithValue0:_sleepViewModel.deepSleepingTime value1:_sleepViewModel.simpleSleepingTime value2:_sleepViewModel.activeTime];
}

- (void)animation {
    
//    [_circle0 startAnimationWithAngle:270];
}

- (void)setSteps_str:(NSString *)steps_str {
    
    _steps_str = steps_str;
    NSString *full = [NSString stringWithFormat:@"%@步",_steps_str];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:full];
    [string addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:56],
                            NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, _steps_str.length)];
    [string addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],
                            NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(_steps_str.length, 1)];
    _stepsCount_day.attributedText = string;
}

- (void)setTarget_str:(NSString *)target_str {
    
    _target_str = target_str;
    NSString *full = [NSString stringWithFormat:@"目标%@步",_target_str];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:full attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                                                                                            NSForegroundColorAttributeName:[UIColor grayColor]}];
    _target_day.attributedText = string;
}

- (void)setHour_str:(NSString *)hour_str {
    
    _hour_str = hour_str;
    NSString *full = [NSString stringWithFormat:@"%@时%@分",_hour_str,_minute_str];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:full];
    [string addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:56],
                            NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0, _hour_str.length)];
    [string addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],
                            NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(_hour_str.length, 1)];
    [string addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:56],
                            NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(_hour_str.length+1, _minute_str.length)];
    [string addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],
                            NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(_hour_str.length+_minute_str.length+1, 1)];
    _interval_night.attributedText = string;
}

- (void)setMinute_str:(NSString *)minute_str {
    
    _minute_str = minute_str;
    NSString *full = [NSString stringWithFormat:@"%@时%@分",_hour_str,_minute_str];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:full];
    [string addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:56],
                            NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0, _hour_str.length)];
    [string addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],
                            NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(_hour_str.length, 1)];
    [string addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"AppleSDGothicNeo-Thin" size:56],
                            NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(_hour_str.length+1, _minute_str.length)];
    [string addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],
                            NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(_hour_str.length+_minute_str.length+1, 1)];
    _interval_night.attributedText = string;
}

//- (void)setSleepDetail:(NSArray *)sleepDetail {
//    
//    _sleepDetail = sleepDetail;
//    for (int i = 0; i < 3; i++) {
//        UILabel *label = (UILabel *)[self.view viewWithTag:1100+i];
//        
//    }
//}

- (void)setDeepSleep:(NSString *)deepSleep {
    
    _deepSleep = deepSleep;
    UILabel *label = (UILabel *)[self.view viewWithTag:1100];
    NSString *full = [NSString stringWithFormat:@"%02ld时%02ld分",[_deepSleep integerValue]/60,[_deepSleep integerValue]%60];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0, 2)];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:9],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(2, 1)];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(3, 2)];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:9],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(5, 1)];
    label.attributedText = attribute;
}

- (void)setLightSleep:(NSString *)lightSleep {
    
    _lightSleep = lightSleep;
    UILabel *label = (UILabel *)[self.view viewWithTag:1101];
    NSString *full = [NSString stringWithFormat:@"%02ld时%02ld分",[_lightSleep integerValue]/60,[_lightSleep integerValue]%60];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0, 2)];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:9],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(2, 1)];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(3, 2)];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:9],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(5, 1)];
    label.attributedText = attribute;
}

- (void)setActiveSleep:(NSString *)activeSleep {
    
    _activeSleep = activeSleep;
    UILabel *label = (UILabel *)[self.view viewWithTag:1102];
    NSString *full = [NSString stringWithFormat:@"%02ld时%02ld分",[_activeSleep integerValue]/60,[_activeSleep integerValue]%60];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0, 2)];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:9],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(2, 1)];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(3, 2)];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:9],
                               NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(5, 1)];
    label.attributedText = attribute;
}

#define defaultcolor [UIColor colorWithWhite:0 alpha:0.7]

- (void)setCalorie_str:(NSString *)calorie_str {
    
//    _calorie_str = calorie_str;
    NSString *full = [NSString stringWithFormat:@"热量消耗\n%@克肥肉",_fat_str];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:15];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} range:NSMakeRange(4, attribute.length - 7)];
    [attribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attribute length])];
    [attribute addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithWhite:0.5 alpha:1]} range:NSMakeRange(0, 4)];
    _calorie.attributedText = attribute;
}

- (void)setFat_str:(NSString *)fat_str {
    
    _fat_str = fat_str;
    NSString *full = [NSString stringWithFormat:@"热量消耗\n%@克肥肉",_fat_str];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:10];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} range:NSMakeRange(4, attribute.length - 7)];
    [attribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attribute length])];
    [attribute addAttributes:@{NSForegroundColorAttributeName:defaultcolor} range:NSMakeRange(0, 4)];
    _calorie.attributedText = attribute;
    _calorie.textAlignment = NSTextAlignmentCenter;
}

- (void)setDistance_str:(NSString *)distance_str {
    
    _distance_str = distance_str;
    
    NSString *full = [NSString stringWithFormat:@"步行距离\n%@千米",_distance_str];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:10];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} range:NSMakeRange(4, attribute.length - 6)];
    [attribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attribute length])];
    [attribute addAttributes:@{NSForegroundColorAttributeName:defaultcolor} range:NSMakeRange(0, 4)];
    _distance.attributedText = attribute;
    _distance.textAlignment = NSTextAlignmentCenter;
}

- (void)setTemperatureValue:(NSString *)temperature_str {
    int i = 4;
    NSString *full = [NSString stringWithFormat:@"实时温度\n%@°",temperature_str];
    if ([temperature_str isEqualToString:@"--"]) {
        
        full = [NSString stringWithFormat:@"实时温度\n%@",temperature_str];
        i = (int)full.length;
    }

    
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:10];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} range:NSMakeRange(4, attribute.length - i)];
    [attribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attribute length])];
    [attribute addAttributes:@{NSForegroundColorAttributeName:defaultcolor} range:NSMakeRange(0, 4)];
    _temperature.attributedText = attribute;
    _temperature.textAlignment = NSTextAlignmentCenter;
}

- (void)setGlassValue:(NSString *)glass_str {
    int i = 4;
    NSString *full = [NSString stringWithFormat:@"气压\n%@百帕",glass_str];
    if ([_elevation.text rangeOfString:@"--"].length > 0) {
        full = [NSString stringWithFormat:@"气压\n--"];
        i = (int)full.length;
    }
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:10];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} range:NSMakeRange(2, attribute.length - i)];
    [attribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attribute length])];
    [attribute addAttributes:@{NSForegroundColorAttributeName:defaultcolor} range:NSMakeRange(0, 2)];
    _glass.attributedText = attribute;
    _glass.textAlignment = NSTextAlignmentCenter;
}


- (void)setElevationValue:(NSString *)elevation_str {
    int i = 3;
    NSString *full = [NSString stringWithFormat:@"海拔\n%@米",elevation_str];
    if ([elevation_str isEqualToString:@"0.0"]) {
        
        full = [NSString stringWithFormat:@"海拔\n--"];
        i = (int)full.length;
    }
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:10];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} range:NSMakeRange(2, attribute.length - i)];
    [attribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attribute length])];
    [attribute addAttributes:@{NSForegroundColorAttributeName:defaultcolor} range:NSMakeRange(0, 2)];
    _elevation.attributedText = attribute;
    _elevation.textAlignment = NSTextAlignmentCenter;
}

- (void)setMyLocationValue:(NSString *)myLocation_str weather:(NSString *)weather{

    NSString *full = [NSString stringWithFormat:@"%@\n%@",myLocation_str,weather];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:full];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:10];
    [attribute addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14]} range:NSMakeRange(attribute.length - weather.length, weather.length)];
    [attribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attribute length])];
    [attribute addAttributes:@{NSForegroundColorAttributeName:defaultcolor} range:NSMakeRange(0, myLocation_str.length)];
    _myLocation.attributedText = attribute;
    _myLocation.textAlignment = NSTextAlignmentCenter;
}

- (void)requestSuccessWithCityName:(NSString *)city weather:(NSString *)weather temperature:(NSString *)temperature{
    
//    self.weatherView.city = city;
//    self.weatherView.theWeather = weather;
//    self.weatherView.temperature = temperature;
    [self setTemperatureValue:temperature];
    [self setMyLocationValue:city weather:weather];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
